// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;
import BubbleUtil from "./BubbleUtil";
import GameClientData from "./data/GameClientData";
import SimulationManager from "../base/lockStep/SimulationManager";
import InputBehaviour from "./behaviours/InputBehaviour";

@ccclass
export default class GameInputManager extends cc.Component {
    public static instance:GameInputManager = null;
    isPress:boolean = false;

    onTouchStart(event:cc.Event.EventTouch)
    {
        cc.log("GameInputManager|onTouchStart");
        this.isPress = true;
        let touchPoint = cc.v2(event.getLocationX(), event.getLocationY());
        BubbleUtil.touchPointTransform(touchPoint);

        let inputBehaviour = SimulationManager.Instance().GetSimulation(GameClientData.simId).GetBehaviour("InputBehaviour") as InputBehaviour;
        inputBehaviour.AddTouchPoint(touchPoint);
    }

    onTouchEnd(event:cc.Event.EventTouch)
    {
        cc.log("GameInputManager|onTouchEnd");
        this.isPress = false;
        
        let touchPoint = cc.v2(event.getLocationX(), event.getLocationY());
        BubbleUtil.touchPointTransform(touchPoint);

        let inputBehaviour = SimulationManager.Instance().GetSimulation(GameClientData.simId).GetBehaviour("InputBehaviour") as InputBehaviour;
        inputBehaviour.SetTouchEndFlag(true);
        inputBehaviour.AddTouchPoint(touchPoint);
    }

    onTouchMove(event:cc.Event.EventTouch)
    {
        if(!this.isPress) return;
        
        cc.log("GameInputManager|onTouchMove");
        let touchPoint = cc.v2(event.getLocationX(), event.getLocationY());
        BubbleUtil.touchPointTransform(touchPoint);

        let inputBehaviour = SimulationManager.Instance().GetSimulation(GameClientData.simId).GetBehaviour("InputBehaviour") as InputBehaviour;
        inputBehaviour.AddTouchPoint(touchPoint);
    }

    // LIFE-CYCLE CALLBACKS:
    onLoad () {
        GameInputManager.instance = this;

        this.node.on('touchstart', this.onTouchStart, this);
        this.node.on('touchmove', this.onTouchMove, this);
        this.node.on('touchend', this.onTouchEnd, this);
    }

    start () {

    }

    // update (dt) {}
}
